package org.anyin.gitee.cloud.lib.core.web.filter;

import org.anyin.gitee.cloud.lib.core.web.context.WebContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class WebContextFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
            WebContext.init(request, response);

            try {
                filterChain.doFilter(request, response);
            } finally {
                WebContext.destroy();
            }

        }
    }

    public void destroy() {
    }
}
